package com.tbit.uqbike.service.business.impl;

import cn.hutool.core.io.FileUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tbit.uqbike.object.pojo.Account;
import com.tbit.uqbike.object.pojo.Coupon;
import com.tbit.uqbike.object.pojo.RechargeGiveIncomeQuery;
import com.tbit.uqbike.object.pojo.dto.*;
import com.tbit.uqbike.object.pojo.vo.RechargeGiveDetailVO;
import com.tbit.uqbike.object.pojo.vo.RechargeGiveIncomeVO;
import com.tbit.uqbike.object.pojo.vo.RechargeGiveTotalDetailVO;
import com.tbit.uqbike.object.pojo.vo.RechargeGiveTotalIncomeVO;
import com.tbit.common.entity.view.PageResult;
import com.tbit.uqbike.service.business.RechargeGiveStatService;
import com.tbit.uqbike.webmanager.dao.core.AccountDao;
import com.tbit.uqbike.webmanager.dao.core.CouponDao;
import com.tbit.uqbike.webmanager.dao.log.RechargeGiveLogDao;
import com.tbit.uqbike.webmanager.util.BeanUtils;
import com.tbit.uqbike.webmanager.util.StringUtil;
import com.tbit.uqbike.webmanager.util.csv.CsvExportUtils;
import com.tbit.uqbike.webmanager.util.csv.entity.CsvBaseEntity;
import com.tbit.uqbike.webmanager.util.csv.entity.ExportHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * @AUTHOR: wei.xing
 * @DATE: 2022-11-23 17:13
 **/
@Service
public class RechargeGiveStatServiceImpl implements RechargeGiveStatService {

    @Autowired
    RechargeGiveLogDao dao;
    @Autowired
    CsvExportUtils csvExportUtils;
    @Autowired
    AccountDao accountDao;
    @Autowired
    CouponDao couponDao;

    @Override
    public RechargeGiveTotalDetailVO getTotalDetail(StatDetailDTO dto) {
        List<Integer> accountIds = StringUtil.getIntegerList(dto.getAccountIds());
        return dao.getRechargeGiveTotalDetail(accountIds, dto.getStartTime(), dto.getEndTime());
    }

    @Override
    public PageResult<RechargeGiveDetailVO> getDetailPage(StatDetailPageDTO pageDTO) {
        List<Integer> accountIds = StringUtil.getIntegerList(pageDTO.getAccountIds());
        PageHelper.startPage(pageDTO.getPageNO(), pageDTO.getRowCount());
        List<RechargeGiveDetailVO> list = dao.getRechargeGiveDetailList(accountIds, pageDTO.getStartTime(), pageDTO.getEndTime());
        PageInfo<RechargeGiveDetailVO> pageInfo = new PageInfo(list);
        return new PageResult<>(pageDTO.getPageNO(), pageDTO.getRowCount(), pageInfo.getTotal(), pageInfo.getPages(), pageInfo.getList());
    }

    @Override
    public String exportDetail(StatDetailDownloadDTO downloadDTO) {
        List<Integer> accountIds = StringUtil.getIntegerList(downloadDTO.getAccountIds());

        List<RechargeGiveDetailVO> result = dao.getRechargeGiveDetailList(accountIds, downloadDTO.getStartTime(), downloadDTO.getEndTime());
        //获取临时的路径
        File tempFile = csvExportUtils.getTempFile();
        try {
            File file = csvExportUtils.exportCsv(RechargeGiveDetailVO.class, tempFile, downloadDTO.getFileName(), result);
            String uploadFileName = UUID.randomUUID().toString().replaceAll("-", "");
            //压缩文件并上传oss
            return csvExportUtils.uploadZip(tempFile, uploadFileName, file);
        } finally {
            //删除临时文件
            FileUtil.del(tempFile);
        }
    }

    @Override
    public RechargeGiveTotalIncomeVO getTotalIncome(RechargeGiveIncomeDTO dto) {
        List<Integer> accountIds = StringUtil.getIntegerList(dto.getAccountIds());
        RechargeGiveIncomeQuery query = BeanUtils.copyProperties(dto, RechargeGiveIncomeQuery.class);
        query.setAccountIds(accountIds);
        return dao.getRechargeGiveTotalIncome(query);
    }


    @Override
    public PageResult<RechargeGiveIncomeVO> getIncomePage(RechargeGiveIncomePageDTO pageDTO) {

        List<Integer> accountIds = StringUtil.getIntegerList(pageDTO.getAccountIds());
        RechargeGiveIncomeQuery query = BeanUtils.copyProperties(pageDTO, RechargeGiveIncomeQuery.class);
        query.setAccountIds(accountIds);
        //分页查询充值赠送
        PageHelper.startPage(pageDTO.getPageNO(), pageDTO.getRowCount());
        List<RechargeGiveIncomeVO> list = dao.getRechargeGiveIncomeList(query);
        PageInfo<RechargeGiveIncomeVO> pageInfo = new PageInfo(list);
        //设置区域名称和优惠卷名称
        setOtherName(list);
        return new PageResult<>(pageDTO.getPageNO(), pageDTO.getRowCount(), pageInfo.getTotal(), pageInfo.getPages(), pageInfo.getList());
    }

    /**
     * 设置区域名称和优惠卷名称
     *
     * @param list
     */
    private void setOtherName(List<RechargeGiveIncomeVO> list) {
        if (!list.isEmpty()) {
            //查询区域
            List<Integer> newAccountIds = list.stream().map(v -> v.getAccountId()).distinct().collect(Collectors.toList());
            List<Account> accountList = accountDao.getByAccountIds(newAccountIds);
            //查询优惠卷
            List<Integer> couponIds = list.stream().map(v -> v.getCouponId()).distinct().collect(Collectors.toList());
            List<Coupon> couponList = couponDao.getListByIds(couponIds);

            Map<Integer, Account> accountMap = accountList.stream().collect(Collectors.toMap(Account::getAccountId, v -> v));
            Map<Integer, Coupon> couponMap = couponList.stream().collect(Collectors.toMap(v -> v.getCouponId(), v -> v));
            //设置值
            list.forEach(v -> {
                Account account = accountMap.get(v.getAccountId());
                Coupon coupon = couponMap.get(v.getCouponId());
                if (account != null) {
                    v.setAccountName(account.getName());
                }
                if (coupon != null) {
                    v.setCouponName(coupon.getName());
                }
            });
        }
    }

    @Override
    public String exportIncome(RechargeGiveIncomeDownloadDTO downloadDTO) {

        //获取临时的路径
        File tempFile = csvExportUtils.getTempFile();
        try {
            File file = exportIncome(downloadDTO, tempFile);
            String uploadFileName = UUID.randomUUID().toString().replaceAll("-", "");
            //压缩文件并上传oss
            return csvExportUtils.uploadZip(tempFile, uploadFileName, file);
        } finally {
            //删除临时文件
            FileUtil.del(tempFile);
        }
    }

    public File exportIncome(RechargeGiveIncomeDownloadDTO downloadDTO, File tempFile) {
        //自定义导出处理器
        ExportHandler exportHandler = new ExportHandler() {
            @Override
            public List handle(CsvBaseEntity baseEntity) {
                List<Integer> accountIds = StringUtil.getIntegerList(downloadDTO.getAccountIds());
                RechargeGiveIncomeQuery query = BeanUtils.copyProperties(downloadDTO, RechargeGiveIncomeQuery.class);
                query.setAccountIds(accountIds);
                //设置时间
                query.setStartTime(baseEntity.getStartTime());
                query.setEndTime(baseEntity.getEndTime());
                List<RechargeGiveIncomeVO> list = dao.getRechargeGiveIncomeList(query);
                //设置区域名称和优惠卷名称
                setOtherName(list);
                return list;
            }
        };
        Date startTime = downloadDTO.getStartTime();
        Date endTime = downloadDTO.getEndTime();
        //多线程导出csv文件
        return csvExportUtils.exportBigCsv(exportHandler, RechargeGiveIncomeVO.class, startTime, endTime, tempFile, downloadDTO.getFileName());
    }

}
